// @algorithm @lc id=406 lang=cpp 
// @title queue-reconstruction-by-height


// @test([[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]])=[[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
// @test([[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]])=[[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
class Solution {
public:
    vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
        sort(people.begin(), people.end(),
            [](const vector<int>&a, const vector<int>&b){
                return a[0]>b[0] || a[0]==b[0]&&a[1]<b[1];
            });
        vector< vector<int> > que;
        for(auto & p : people)
            que.insert(que.begin()+p[1], p);
        return que;
    }
};